from django.db import models


# Create your models here.
"""
基本模型创建示例
"""


class Person(models.Model):
    SHIRT_SIZE = (('S', 'Small'), ('M', 'Medium'), ('L', 'Large'))
    name = models.CharField(name="custom name", max_length=60, null=True)
    shirt_size = models.CharField(max_length=1, choices=SHIRT_SIZE, default='S')


class Musician(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    instrument = models.CharField(max_length=100)


class Album(models.Model):
    artist = models.ForeignKey(Musician, verbose_name="the musician")
    name = models.CharField(max_length=100)
    release_date = models.DateField()
    num_starts = models.IntegerField()


class Fruit(models.Model):
    name = models.CharField(max_length=100, primary_key=True)


class Topping(models.Model):
    pass


class Pizza(models.Model):
    toppings = models.ManyToManyField(Topping)


class People(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name


class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(People, through="Membership")

    def __str__(self):
        return self.name


class Membership(models.Model):
    people = models.ForeignKey(People)
    group = models.ForeignKey(Group)
    date_joined = models.DateField(auto_now_add=True)
    invite_reason = models.CharField(max_length=64)

    def __str__(self):
        return "people=" + self.people + ",group=" + self.group + ",date_joined=" + self.date_joined + ",invite_reason=" + self.invite_reason


class Person1(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    birth_date = models.DateField()

    def baby_boomer_status(self):
        import datetime
        if self.birth_date < datetime.date(1945, 8, 1):
            return "Pre-boomer"
        elif self.birth_date < datetime.date(1965, 1, 1):
            return "Baby boomer"
        else:
            return "Post-boomer"

    def _get_full_name(self):
        return '%s %s' % (self.first_name, self.last_name)

    full_name = property(_get_full_name)


class CommonInfo(models.Model):
    """
    抽象基类模型
    """
    name = models.CharField(max_length=100)
    age = models.PositiveIntegerField()

    class Meta:
        abstract = True


class Student(CommonInfo):
    home_group = models.CharField(max_length=5)


class CommonInfo1(models.Model):
    """
    元继承
    """
    name = models.CharField(max_length=100)
    age = models.PositiveIntegerField()

    class Meta:
        abstract = True
        ordering = ['name']


class Student1(CommonInfo1):
    home_group = models.CharField(max_length=5)

    class Meta(CommonInfo1.Meta):
        db_table = 'student_info'


class Place(models.Model):
    name = models.CharField(max_length=50)
    address = models.CharField(max_length=80)


class Restaurant(Place):
    serves_hot_dogs = models.BooleanField(default=False)
    serves_pizza = models.BooleanField(default=False)


class Piece(models.Model):
    pass


class Article(Piece):
    article_to_piece = models.OneToOneField(Piece, parent_link=True)
    headline = models.CharField(max_length=50)
    body = models.TextField()

    def __str__(self):
        return "headline="+self.headline+",body="+self.body


class Book(Piece):
    book_to_piece = models.OneToOneField(Piece, parent_link=True)
    title = models.CharField(max_length=50)

    def __str__(self):
        return "title="+self.title


class BookReview(Book, Article):
    pass

    def __str__(self):
        return "headline="+self.headline+",body="+self.body+",title="+self.title
